package com.ksc.storekeeper.client.view.components;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.HasBlurHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasCaption;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.ksc.storekeeper.client.resources.StoreKeeperResources;
import com.ksc.storekeeper.client.view.validation.ValidatingPasswordTextBox;
import com.ksc.storekeeper.client.view.validation.Validation;

public class FormItemPasswordBox extends Composite implements HasCaption, HasBlurHandlers {

	private static FormItemPasswordBoxUiBinder uiBinder = GWT.create(FormItemPasswordBoxUiBinder.class);

	interface FormItemPasswordBoxUiBinder extends UiBinder<Widget, FormItemPasswordBox> {
	}

	@UiField
	HTMLPanel panel;
	@UiField
	Label label;
	@UiField
	ValidatingPasswordTextBox input;
	@UiField
	InlineHTML error;
	private final String regex = "";
	private boolean validation = false;
	private boolean required = false;
	private boolean alpha = false;
	private boolean numeric = false;
	private boolean alphanumeric = false;

	public FormItemPasswordBox() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public String getCaption() {
		return label.getText();
	}

	/**
	 * Set caption/label for the item
	 */
	@Override
	public void setCaption(String text) {
		label.setText(text);
	}

	/**
	 * Perform Validate
	 * 
	 * @return
	 */
	public boolean validate() {
		boolean valid = true;
		if (validation) {
			input.setRequired(required);
			input.setAlphaNumeric(alphanumeric);
			input.setAlpha(alpha);
			input.setNumeric(numeric);
			valid = input.validatePassword().equals(Validation.VALID) && valid;
			error.setText(input.getErrorMessage());
		}
		return valid;
	}

	public boolean validatePin() {
		boolean valid = true;
		if (validation) {
			input.setRequired(required);
			input.setNumeric(numeric);
			valid = input.validatePin().equals(Validation.VALID) && valid;
			error.setText(input.getErrorMessage());
		}
		return valid;
	}

	public void onBlurValidatePin() {
		boolean valid = true;
		if (regex != null && !regex.isEmpty() && validation) {
			input.setRequired(required);
			valid = input.validateOnBlur().equals(Validation.VALID) && valid;
			error.setText(input.getErrorMessage());
		}
		if (regex == null || regex.isEmpty()) {
			validatePin();
		}
		if (input.getText().isEmpty() && !required) {
			error.setText("");
			input.removeInvalid();
		}
	}

	/**
	 * Get item value
	 * 
	 * @return
	 */
	public String getText() {
		return input.getText();
	}

	/**
	 * Set value to item
	 * 
	 * @param text
	 */
	public void setText(String text) {
		input.setText(text);
	}

	/**
	 * Set parameter validate as true or false
	 * 
	 * @param value
	 */
	public void setValidation(boolean value) {
		validation = value;
	}

	/**
	 * Set parameter required as true or false
	 * 
	 * @param value
	 */
	public void setRequired(boolean value) {
		required = value;
	}

	/**
	 * Set parameter alphabetic as true or false
	 * 
	 * @param value
	 */
	public void setAlphabetic(boolean value) {
		alpha = value;
	}

	/**
	 * Set parameter numeric as true or false
	 * 
	 * @param value
	 */
	public void setNumeric(boolean value) {
		numeric = value;
	}

	/**
	 * Set alpha numeric
	 * 
	 * @param value
	 */
	public void setAlphaNumeric(boolean value) {
		alphanumeric = value;
	}

	/**
	 * Set parameter maxLength value
	 * 
	 * @param value
	 */
	public void setMaximumLength(int length) {
		input.setMaxLength(length);
	}

	/**
	 * Set parameter minLength value
	 * 
	 * @param value
	 */
	public void setMinimunLength(int length) {
		input.setMinLength(length);
	}

	public void setPanelStyle(String style) {
		panel.setStyleName(style);
	}

	public void setItemStyle(String style) {
		input.setStyleName(style);
	}

	public void setErrorStyle(String style) {
		error.setStyleName(style);
	}

	public void setLabelStyle(String style) {
		label.setStyleName(style);
	}

	public void setErrorText(String text) {
		input.addStyleName(StoreKeeperResources.INSTANCE.storeKeeperCss().error());
		error.setText(text);
	}

	public void clear() {
		error.setText("");
		setText("");
		input.removeInvalid();
	}

	@Override
	public HandlerRegistration addBlurHandler(BlurHandler handler) {
		return input.addBlurHandler(handler);
	}

}
